package com.grape.service.system.impl;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.persistence.FetchType;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.NumberUtils;

import com.grape.dao.system.RoleRepository;
import com.grape.dao.system.UserRepository;
import com.grape.domain.system.Role;
import com.grape.domain.system.User;
import com.grape.service.system.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {

	@Autowired
	private UserRepository ur;
	
	@Autowired
	private RoleRepository rr;

	@Override
	public User findByUsername(String username) {
		// 我在domain取消了懒加载，在每个manytomany上加了fetch=FetchType.EAGER
		// 将这个user存进session后，当需要权限校验时直接从user取
		// 如果没有取消懒加载我就需要在权限校验时使用hql级联查询了
		// from Role r inner join fetch r.users u where u.id=? 根据用户id返回关联角色集合
		// from Permission p inner join fetch p.roles r inner join fetch r.user u where
		// u.id=? // 基于上一条hql在级联查询权限
		User user = ur.findByUsername(username);

		return user;
	}

	@Override
	public Page<User> findAll(Pageable pageable, User model,Date startTime,Date endTime) {
		Specification<User> specification = new Specification<User>() {
			
			@Override
			public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<>();
				
				if (StringUtils.isNotBlank(model.getUsername())) {
					Predicate p1 = cb.like(root.get("username").as(String.class),"%"+model.getUsername()+"%");
					list.add(p1);
				}
				
				if (StringUtils.isNotBlank(model.getGender())) {
					Predicate p2 = cb.equal(root.get("gender").as(String.class),model.getGender());
					list.add(p2);
				}
				
				if (startTime!=null) {
					System.out.println("添加出生日期条件+"+startTime);
					Predicate p3 = cb.greaterThanOrEqualTo(root.get("birthday").as(Date.class), startTime); 
					list.add(p3);
				}
				
				if (endTime!=null) {
					Predicate p4 = cb.lessThanOrEqualTo(root.get("birthday").as(Date.class), endTime);  
					
					// 这些个api都是在哪查到的
					// cb.between(root.<Date>get("birthday"), startTime, endTime);
					list.add(p4);
				}
				
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		
		return ur.findAll(specification,pageable);
		
	}

	@Override
	public void save(User model,String[] roleIds) {
		ur.save(model);
		
		for (String string : roleIds) {
			Integer id = Integer.parseInt(string);
			Role findOne = rr.findOne(id);
			if (findOne!=null) {
				model.getRoles().add(findOne);
			}
		}
		
		
	}



}
